package mastermind.tools;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import mastermind.Application;
import mastermind.dataholder.Row;
import mastermind.gui.contents.GameUI;
import mastermind.logic.Game;
import mastermind.logic.Game.GameState;
import mastermind.logic.Game.GameType;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;

/**
 * Diese Klasse verwaltet laden und speichern von Spielst&auml;nden
 */
public class Helper
{

	/**
	 * L&auml;dt ein gespeichertes Spiel
	 */
	public static void loadGame()
	{
		FileDialog dialog = new FileDialog(Application.getShell(), SWT.OPEN);
		String[] filterNames = new String[] { "Mastermind Spiel (.mastermind)" };
		String[] filterExtensions = new String[] { "*.mastermind" };

		dialog.setFilterNames(filterNames);
		dialog.setFilterExtensions(filterExtensions);
		String file = dialog.open();
		if (file != null)
			loadGame(file);
	}

	/**
	 * L&auml;dt ein gespeichertes Spiel aus der angegebenen Datei
	 * 
	 * @param mmfile
	 *            Vollst&auml;ndiger Pfad zur Datei die geladen werden soll
	 */
	private static void loadGame(String mmfile)
	{
		File Spieldaten = new File(mmfile);
		Properties data = new Properties();

		try
		{
			BufferedInputStream bStream = new BufferedInputStream(new FileInputStream(Spieldaten));
			data.load(bStream);
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return;
		}

		String enumGameType = data.getProperty("GameType");
		GameType gameType;

		if (enumGameType.equals("USER"))
		{
			gameType = GameType.USER;
		}
		else if (enumGameType.equals("PC"))
		{
			gameType = GameType.PC;
		}
		else
		{
			gameType = GameType.TWO_PLAYER;
		}

		GameUI.reset();
		Game.setGameState(GameState.LOAD);
		Game.init(Byte.parseByte(data.getProperty("CountColors")),
				Byte.parseByte(data.getProperty("CountHoles")), gameType);
		String hist = data.getProperty("History");
		String[] hRow = hist.split(",");
		for (int i = 0; i < hRow.length; i++)
		{
			if (hRow[i].length() > 0)
			Game.addRow(new Row(hRow[i]));
		}

		Game.setRow(new Row(data.getProperty("CurRow")));
		Game.setSolution(new Row((data.getProperty("Solution"))));
	}

	/**
	 * Speichert einen Spielstand
	 */
	public static void saveGame()
	{
		FileDialog dialog = new FileDialog(Application.getShell(), SWT.SAVE);
		String[] filterNames = new String[] { "Mastermind Spiel (.mastermind)" };
		String[] filterExtensions = new String[] { "*.mastermind" };

		dialog.setFilterNames(filterNames);
		dialog.setFilterExtensions(filterExtensions);
		String file = dialog.open();
		if (file != null)
			saveGame(file);

	}

	/**
	 * Speichert einen Spielstand in die angegebene Datei
	 * 
	 * @param path
	 *            Datei die gespeichert werden soll.<br/>
	 *            Wird wenn bereits vorhandenn &uuml;berschrieben
	 */
	private static void saveGame(String path)
	{
		String cntHoles = Byte.toString(Game.getCntHoles());
		String cntColors = Byte.toString(Game.getCntColors());
		String gameType = Game.getGameType().toString();
		String history = "";
		for (int i = 0; i < Game.getHistory().size(); i++)
		{
			history += "," + Game.getHistory().get(i);
		}
		if (history.length() > 0)
			history = history.substring(1);
		String curRow = Game.getCurRow().toString();
		String solution = Game.getSolution().toString();

		Properties data = new Properties();
		data.setProperty("CountHoles", cntHoles);
		data.setProperty("CountColors", cntColors);
		data.setProperty("CurRow", curRow);
		data.setProperty("GameType", gameType);
		data.setProperty("History", history);
		data.setProperty("Solution", solution);

		File file = new File(path);

		try
		{
			data.store(new FileOutputStream(file), "Daten des Mastermind-Spiels");
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}
